<?php

declare(strict_types=1);

namespace app\admin\controller;

use app\admin\model\Table as mod;

/**
 * 数据表 class
 */
class Table
{
    /**
     * 查询表 function
     *
     * @return void
     */
    public function query()
    {
        if (request()->isPost()) {
            $param = request()->param();
            $databese = config("database.connections.mysql.database");
            if ($param["type"] == "table") {
                $res = mod::query($databese);
                return json(["code" => 0, "data" => $res, "msg" => "获取成功"]);
            } else {
                $res = mod::fields(
                    databese: $databese,
                    table: $param["table"]
                );
                return json(["code" => 0, "data" => $res, "msg" => "获取成功"]);
            }
        }
        return view();
    }

    /**
     * 创建表 function
     *
     * @return void
     */
    public function create()
    {
        if (request()->isPost()) {
            $param = request()->param();
            $res = mod::create($param);
            return json(["code" => 0, "data" => $res, "msg" => "操作成功"]);
        }
        return view();
    }

    /**
     * 更新表 function
     *
     * @return void
     */
    public function update()
    {
        $id = request()->get("id");
        $databese = config("database.connections.mysql.database");
        $find = mod::find(databese: $databese, table: $id);
        if (request()->isPost()) {
            $param = request()->param();
            $res = mod::update($param);
            return json(["code" => 0, "data" => $res, "msg" => "操作成功"]);
        }
        return view(vars: ["find" => $find]);
    }

    /**
     * 删除表 function
     *
     * @return void
     */
    public function delete()
    {
        $id = request()->param("id");
        mod::delete($id);
        return json(["code" => 0, "msg" => "操作成功"]);
    }

    /**
     * 创建字段 function
     *
     * @return void
     */
    public function create_field()
    {
        if (request()->isPost()) {
            $param = request()->param();
            $res = mod::create_field($param);
            return json(["code" => 0, "data" => $res, "msg" => "操作成功"]);
        }
        return view();
    }

    /**
     * 更新字段 function
     *
     * @return void
     */
    public function update_field()
    {
        $id = request()->get("id");
        $ab = request()->get("ab");
        $find = mod::field_find($id, $ab);
        if (request()->isPost()) {
            $param = request()->param();
            $res = mod::update_field($param);
            return json(["code" => 0, "data" => $res, "msg" => "操作成功"]);
        }
        return view(vars: ["find" => $find]);
    }

    /**
     * 删除字段 function
     *
     * @return void
     */
    public function delete_field()
    {
        $id = request()->param("id");
        $ab = request()->param("ab");
        mod::delete_field($id, $ab);
        return json(["code" => 0, "msg" => "操作成功"]);
    }

    /**
     * 生成表单 function
     *
     * @return void
     */
    public function build()
    {
        $id = request()->param('id');
        $fields = mod::get_fields($id);

        if (request()->isPost()) {
            $param = request()->param();
            mod::build($param);
            return json(["code" => 0, "msg" => "操作成功"]);
        }
        return view(vars: ["fields" => $fields]);
    }
}
